<template>
  <view class="container">
    
    <view class="listbox">
      <view class="product" v-for="(item,index) in list"  :key="index" @click="goDetail(item.sku_id)">
        <image :src="$util.img(item.goods_image.split(',')[0], { size: 'mid' })" mode="aspectFill" class="img" lazy-load></image>
        <view class="info">
          <view class="product-name ellipsis-2">{{item.sku_name}}</view>
          <view class="flex-between">
              <view class="font-32 c-origin flex-y-baseline text-bold">
                <text class="font-24">¥</text>{{ item.discount_price }}
              </view>
            <view class="flex-y-center font-24 c-78" @click.stop="editCollection(item.goods_id)">
              <image :src="$util.img('/image/address_del_grey.png')" mode="" class="del"></image>删除
            </view>
          </view>
        </view>
      </view>
    </view>
    
    
    <emptyData :width="empty.width" :height="empty.height" :text="empty.text" :icon="empty.icon" :marginTop="empty.marginTop" v-if="list.length == 0"></emptyData>
    
  </view>
</template>

<script>
  export default {
    data() {
      return {
        empty: {
          width: 135,
          height: 134,
          text: '暂无收藏的商品',
          icon: '/image/emptyAddress.png',
          marginTop: 200
        },
        current: 0,
        page: 1,
        list: []
      }
    },
    onShow() {
      this.page = 1;
      this.getlist()
    },
    onReachBottom() {
      this.page ++ ;
      this.getlist()
    },
    methods: {
      editCollection(e) {
        this.$api.sendRequest({
          url: "/api/goodscollect/delete",
          data: {
            goods_id: e
          },
          success: res => {
            if (res.code == 0) {
                this.$util.showToast({
                  title: "删除成功"
                })
                let array = this.list;
                let newArray = array.filter((v) => {
                  return v.goods_id != e;
                })
                this.list = newArray;
            } else {
                this.$util.showToast({
                  title: res.message
                })
            }
          }
        });
      },
      goDetail(sku_id) {
        this.$util.redirectTo('/otherpages/mall/detail?sku_id='+sku_id)
      },
      getlist() {
        this.$api.sendRequest({
        	url: "/api/goodscollect/page",
        	data: {
            page: this.page,
            page_size: 20,
        	},
        	success: res => {
            if(res.code == 0) {
              if(this.page == 1) this.list = [];
              this.list = [...this.list, ...res.data.list]
            }else {
              this.$util.showToast({title: res.message})
            }
        		
        	}
        });
      },
    }
  }
</script>

<style lang="scss" scoped>
.tabs {
  display: flex;
  margin: 24rpx;
  justify-content: space-around;
  .tabsitem {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 20rpx;
    background-color: #fff;
    width: 312rpx;
    height: 66rpx;
  }
  .active {
    color: #fff!important;
    font-weight: bold;
    background-color: $color-title2!important;
  }
}
.listbox {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 24rpx 24rpx 0;
  
  .product {
    display: flex;
    flex-direction: column;
    width: 330rpx;
    border-radius: $border-radius;
    background: #fff;
    margin-bottom: 30rpx;
    
    .img {
      width: 330rpx;
      height: 330rpx;
      border-radius: 20rpx 20rpx 0 0;
    }
    
    
    .info {
      padding: 20rpx 20rpx 10rpx;
      .product-name {
        height: 78rpx;
        line-height: 1.4;
      }
      .del {
        width: 24rpx;
        height: 24rpx;
        margin-right: 4rpx;
      }
    }
    
  }
}
</style>
